package com.qili.util;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author zhaoyongke
 * @date 2020/11/4
 * @description  文件导出工具类
 */

public class ExportFileUtil {


    /**
    * @Author:zhaoyongke
    * @Description:
    * @params * @param response
    * @param filePath
    * @Date:15:10 2020/11/4
    */
    public static void exportFile(HttpServletResponse response, HttpServletRequest   request,String filePath){
        File file = new File(filePath);
        // 取得文件名。
        String fileName = file.getName();
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
            String agent = request.getHeader("User-Agent");
            boolean isMSIE = (agent != null && agent.indexOf("Trident") != -1);
            String finalFileName="";
            if( isMSIE ){
                 finalFileName = java.net.URLEncoder.encode(fileName,"UTF8");
            }else{
                 finalFileName = new String(fileName.getBytes("utf-8"), "iso8859-1");
                 // 这里中文乱码了

            }
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/force-download");// 设置强制下载不打开

            response.addHeader("Content-Disposition",
                    "attachment;filename=" + finalFileName);
            response.setHeader("Content-Length", String.valueOf(file.length()));

            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) != -1) {
                response.getOutputStream().write(b, 0, len);
            }
            response.flushBuffer();
            fis.close();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static void exportFileNew(HttpServletResponse response, HttpServletRequest   request,String filePath,String fileName){
        File file = new File(filePath);
        // 取得文件名。
        if(StringUtil.isBlank(fileName)){
            fileName = file.getName();
        }
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
            ServletOutputStream outputStream = response.getOutputStream();
            String agent = request.getHeader("User-Agent");
            boolean isMSIE = (agent != null && agent.indexOf("Trident") != -1);
            String finalFileName="";
            if( isMSIE ){

                finalFileName = java.net.URLEncoder.encode(fileName,"UTF8");
            }else{
                finalFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            }
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/force-download");// 设置强制下载不打开

            response.addHeader("Content-Disposition",
                    "attachment;filename=" + finalFileName);
            response.setHeader("Content-Length", String.valueOf(file.length()));

            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) != -1) {
                outputStream.write(b, 0, len);
            }
            response.flushBuffer();
            fis.close();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Author:zhaoyongke
     * @Description:
     * @params * @param response
     * @param filePath
     * @Date:15:10 2020/11/4
     */
    public static void exportFileApp(HttpServletResponse response, HttpServletRequest   request,String filePath){
        File file = new File(filePath);
        // 取得文件名。
        String fileName = file.getName();
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
            String agent = request.getHeader("User-Agent");
            boolean isMSIE = (agent != null && agent.indexOf("Trident") != -1);
            String finalFileName="";
            if( isMSIE ){
                finalFileName = java.net.URLEncoder.encode(fileName,"UTF8");
            }else{
                finalFileName = new String(fileName.getBytes("utf-8"), "iso8859-1");
            }
            response.reset();
            response.setCharacterEncoding("UTF-8");
//            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Access-Control-Allow-Origin", "*");
            response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            response.addHeader("Access-Control-Allow-Headers", "Content-Type");
            response.addHeader("Content-Disposition",
                    "attachment;filename=" + finalFileName);
            response.setHeader("Content-Length", String.valueOf(file.length()));


            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) != -1) {
                response.getOutputStream().write(b, 0, len);
            }
//            response.setContentType("application/octet-stream");
            response.flushBuffer();
            fis.close();

//            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

//            toClient.write(buffer);
//            toClient.flush();
//            toClient.close();

        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}




